// Copyright (c) BruTile developers team. All rights reserved. See License.txt in the project root for license information.

using System.Xml;
using System.Xml.Linq;

namespace BruTile.Wms;

public class FeatureListURL : XmlObject
{
    private OnlineResource _onlineResourceField;

    public FeatureListURL()
    { }

    public FeatureListURL(XElement node, string ns)
    {
        var element = node.Element(XName.Get("Format", ns)) ?? throw WmsParsingException.ElementNotFound("Format");
        Format = element.Value;

        element = node.Element(XName.Get("OnlineResource", ns));
        if (element == null)
            throw WmsParsingException.ElementNotFound("OnlineResource");
        OnlineResource = new OnlineResource(element);
    }

    public string Format { get; set; }

    public OnlineResource OnlineResource
    {
        get => _onlineResourceField ??= new OnlineResource();
        set => _onlineResourceField = value;
    }

    #region Overrides of XmlObject

    public override void ReadXml(XmlReader reader)
    {
        reader.MoveToContent();
        if (reader.IsEmptyElement) { reader.Read(); return; }

        while (!reader.EOF)
        {
            if (reader.IsStartElement())
            {
                switch (reader.LocalName)
                {
                    case "Format":
                        Format = reader.ReadElementContentAsString();
                        break;
                    case "OnlineResource":
                        OnlineResource = new OnlineResource();
                        OnlineResource.ReadXml(reader);
                        break;
                    default:
                        reader.Skip();
                        break;
                }
            }
            else
            {
                reader.Read();
            }
        }
    }

    public override void WriteXml(XmlWriter writer)
    {
        writer.WriteElementString("Format", Namespace, Format);
        writer.WriteStartElement("OnlineResource", Namespace);
        OnlineResource.WriteXml(writer);
        writer.WriteEndElement();
    }

    public override XElement ToXElement(string @namespace)
    {
        return new XElement(XName.Get("DataURL", @namespace),
            new XElement(XName.Get("Format", @namespace), Format),
            OnlineResource.ToXElement(@namespace));
    }

    #endregion Overrides of XmlObject
}
